package com.ciwong.epaper.modules.scan.ui;

import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.epaper.R;
import com.ciwong.epaper.bean.SelectObject;
import com.ciwong.epaper.modules.me.adapter.ClassListAdapter;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.mobilelib.utils.ViewHolder;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

/**
 * 拍答题卡Dialog
 *
 * @author bin
 * @version ciwong v.1.0 2015/10/21 10:27
 * @since ciwong v.1.0
 */
public class SelectObjectDialogActivity extends BaseActivity
{
    private List<SelectObject> mSelectObjects;

    private SelectObject mSelectObject;

    private View selectObjectCancel, selectObjectConfirm;

    private ListView selectObjectLV;

    private ClassListAdapter mAdapter;

    @Override
    protected void preCreate()
    {
        super.preCreate();
        Intent intent = getIntent();
        if (intent != null)
        {
            mSelectObjects = (List<SelectObject>) intent
                    .getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ_LIST);
            mSelectObject = (SelectObject) intent
                    .getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ);
        }
    }

    @Override
    protected int setView()
    {
        return R.layout.activity_select_object_dialog;
    }

    @Override
    protected void findViews()
    {
        selectObjectCancel = findViewById(R.id.select_object_cancel);
        selectObjectConfirm = findViewById(R.id.select_object_confirm);
        selectObjectLV = (ListView) findViewById(R.id.select_object_lv);
    }

    @Override
    protected void initEvent()
    {
        selectObjectCancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                finish();
                overridePendingTransition(R.anim.down_bottom_out, 0);
            }
        });

        selectObjectConfirm.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (mSelectObject == null)
                {
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra(IntentFlag.INTENT_FLAG_OBJ, mSelectObject);
                setResult(RESULT_OK, intent);
                finish();
                overridePendingTransition(R.anim.down_bottom_out, 0);
            }
        });


    }

    @Override
    protected void init()
    {
        getWindow().setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams a = getWindow().getAttributes();
        a.width = WindowManager.LayoutParams.MATCH_PARENT;
        a.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(a);
        hideTitleBar();
        mAdapter = new ClassListAdapter(this, mSelectObjects, mSelectObject);
        selectObjectLV.setAdapter(mAdapter);


//        selectObjectLV
//                .setOnItemClickListener(new AdapterView.OnItemClickListener()
//                {
//                    @Override
//                    public void onItemClick(AdapterView<?> parent, View view,
//                                            int position, long id)
//                    {
//
//                        Log.d("xixin","------什么情况--1111------"+parent);
//                        SelectObject selectObject = (SelectObject) parent
//                                .getItemAtPosition(position);
//                        Log.d("xixin","------什么情况--------"+selectObject);
//                        if (selectObject != null)
//                        {
//                            mAdapter.setSelectObject(selectObject);
//                            mSelectObject = selectObject;
//
//
//                            Intent intent = new Intent();
//                            intent.putExtra(IntentFlag.INTENT_FLAG_OBJ, mSelectObject);
//                            setResult(RESULT_OK, intent);
//                            finish();
//                            overridePendingTransition(R.anim.down_bottom_out, 0);
//                        }
//                    }
//                });




    }

    @Override
    protected void loadData()
    {
    }





    class ClassListAdapter extends BaseAdapter
    {
        /**
         * 上下文对象
         */
        private Context mContext;

        /**
         * 班级信息集
         */
        private List<SelectObject> mSelectObjects;

        /**
         * 默认选择的班级
         */
        private SelectObject mSelectObject;

        /**
         * 构造函数
         *
         * @param context
         *            上下文对象
         * @param selectObjects
         *            班级信息集
         * @param mSelectObject
         *            当前选中的学校信息
         */
        public ClassListAdapter(Context context, List<SelectObject> selectObjects,
                                SelectObject mSelectObject)
        {
            this.mContext = context;
            this.mSelectObjects = selectObjects;
            this.mSelectObject = mSelectObject;
        }

        @Override
        public int getCount()
        {
            return mSelectObjects != null ? mSelectObjects.size() : 0;
        }

        @Override
        public Object getItem(int position)
        {
            return mSelectObjects != null && !mSelectObjects.isEmpty()
                    ? mSelectObjects.get(position) : null;
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {

            View v;
            if (convertView == null)
            {
                v = View.inflate(mContext, R.layout.item_class, null);
            }
            else
            {
                v = convertView;
            }
            TextView tvClassName = ViewHolder.get(v, R.id.tv_item_class);

            TextView tv_teacher_name = ViewHolder.get(v, R.id.tv_teacher_name);
            ViewHolder.get(v,R.id.btn_join_class).setVisibility(View.GONE);
            final SelectObject selectObject = (SelectObject) getItem(position);

            tvClassName.setText(selectObject.getName());

//        tv_teacher_name.setText(((Clazz)selectObject).getRoomUserName());
//        ivSelect.setVisibility(this.mSelectObject != null
//                && this.mSelectObject.equals(selectObject) ? View.VISIBLE
//                        : View.INVISIBLE);

            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra(IntentFlag.INTENT_FLAG_OBJ, selectObject);
                    setResult(RESULT_OK, intent);
                    finish();
                    overridePendingTransition(R.anim.down_bottom_out, 0);
                }
            });
            return v;
        }

        public void clear()
        {
            if (this.mSelectObjects != null || !this.mSelectObjects.isEmpty())
            {
                this.mSelectObjects.clear();
                notifyDataSetChanged();
            }
        }

        public void addAll(List<SelectObject> clazzs)
        {
            if (this.mSelectObjects != null)
            {
                this.mSelectObjects.addAll(clazzs);
                notifyDataSetChanged();
            }
        }

        public SelectObject getSelectObject()
        {
            return mSelectObject;
        }


        public void setSelectObject(SelectObject selectObject)
        {
            this.mSelectObject = selectObject;
            notifyDataSetChanged();
        }

    }








}
